package paneles;

import javax.swing.*;

import java.awt.*;

import principal.EstandarxDLG;

import java.awt.Rectangle;
import javax.swing.JTextField;

public class DialogoProgreso extends JDialog {
	private static final long serialVersionUID = 1L;
	
	private static final int MOMENTOS=1;	
	private static final int MAXVEROSIMILITUD=2;
	private static final int PERCENTILES=3;
	
	private JPanel jContentPane = null;
	private JButton cancelar = null;
	public JProgressBar progressBar = null;
	private JLabel tiempo = null;
	public JLabel tiempoTxt = null;
	//se sabe cual metodo se esta usando  
	private EstandarxDLG principal;
	public JTextField consola = null;

	/**
	 * el hilo que se interrumpe va cambiando con ajustarHiloPadre(principal)
	 * @param estandarxDLG
	 * @param hilopinicial
	 */
	public DialogoProgreso(EstandarxDLG estandarxDLG) {
		super();
		initialize();
		this.principal=estandarxDLG;
	}

	public void inicio() {
		try {
			SwingUtilities.invokeLater(new Runnable(){
				public void run() {
					cancelar.setEnabled(true);
					setVisible(true);
					progressBar.setValue(0);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * mostrar parametros en el panel
	 * guarda solucion en salida
	 */
	public void fin(){
		switch (principal.moduloDLG.METODOACTUAL) 
		{
		case MOMENTOS:
                    setVisible(false);
			principal.moduloDLG.MMomentos.ajustarLambdas(principal.moduloDLG.MMomentos.mejorcromo);
			principal.moduloDLG.salida.lambda1=principal.moduloDLG.MMomentos.la1;
			principal.moduloDLG.salida.lambda2=principal.moduloDLG.MMomentos.la2;
			principal.moduloDLG.salida.lambda3=principal.moduloDLG.MMomentos.la3;
			principal.moduloDLG.salida.lambda4=principal.moduloDLG.MMomentos.la4;
			principal.moduloDLG.salida.errorReal=principal.moduloDLG.MMomentos.errorMaxDePoblacion;

			principal.salida=principal.moduloDLG.salida;
//			try {
//				SwingUtilities.invokeAndWait(new Runnable(){
//					public void run() {
//						setVisible(false);
//						principal.getJBEjecutarM().setEnabled(true);
//						principal.getJBEjecutarP().setEnabled(true);
//						principal.getJBEjecutarMax().setEnabled(true);
//						//
//						double momentos[] = principal.moduloDLG.ObtenerMomentos();
//						double erroresm[] = principal.moduloDLG.ObtenerErroresMomentos();
//						principal.jTextMedia.setText(Double.toString(momentos[0]));
//						principal.jTextVarianza.setText(Double.toString(momentos[1]));
//						principal.jTextSimetria.setText(Double.toString(momentos[2]));
//						principal.jTextApuntamiento.setText(Double.toString(momentos[3]));
//						principal.jTextError3.setText(Double.toString(erroresm[0]));
//						principal.jTextError4.setText(Double.toString(erroresm[1]));
//
//						principal.txtmomLa1.setText(Double.toString(principal.salida.lambda1));
//						principal.txtmomLa2.setText(Double.toString(principal.salida.lambda2));
//						principal.txtmomLa3.setText(Double.toString(principal.salida.lambda3));
//						principal.txtmomLa4.setText(Double.toString(principal.salida.lambda4));
//					}});
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
			
			break;

		case PERCENTILES:
			principal.moduloDLG.MPercentiles.ajustarLambdas(principal.moduloDLG.MPercentiles.mejorcromo);
			principal.moduloDLG.salida.lambda1=principal.moduloDLG.MPercentiles.la1;
			principal.moduloDLG.salida.lambda2=principal.moduloDLG.MPercentiles.la2;
			principal.moduloDLG.salida.lambda3=principal.moduloDLG.MPercentiles.la3;
			principal.moduloDLG.salida.lambda4=principal.moduloDLG.MPercentiles.la4;
			principal.moduloDLG.salida.errorReal=principal.moduloDLG.MPercentiles.errorMaxDePoblacion;

			principal.salida=principal.moduloDLG.salida;
			try {
				SwingUtilities.invokeAndWait(new Runnable(){
					public void run() {
						setVisible(false);
						principal.getJBEjecutarM().setEnabled(true);
						principal.getJBEjecutarP().setEnabled(true);
						principal.getJBEjecutarMax().setEnabled(true);
						//
						double rhos[] = principal.moduloDLG.ObtenerRhos();
						double erroresp[] = principal.moduloDLG.ObtenerErroresPercentiles();
						principal.jTextMedianap.setText(Double.toString(rhos[0]));
						principal.jTextInterDecilp.setText(Double.toString(rhos[1]));
						principal.jTextTasaPCp.setText(Double.toString(rhos[2]));
						principal.jTextFactorPCp.setText(Double.toString(rhos[3]));
						principal.jTextError3p.setText(Double.toString(erroresp[0]));
						principal.jTextError4p.setText(Double.toString(erroresp[1]));

						principal.txtPercLa1.setText(Double.toString(principal.salida.lambda1));
						principal.txtPercLa2.setText(Double.toString(principal.salida.lambda2));
						principal.txtPercLa3.setText(Double.toString(principal.salida.lambda3));
						principal.txtPercLa4.setText(Double.toString(principal.salida.lambda4));
					}});
			}catch (Exception e) {
				e.printStackTrace();
			}
			
			break;

		case MAXVEROSIMILITUD:
			principal.moduloDLG.salida.lambda1=principal.moduloDLG.MMaximav.la1;
			principal.moduloDLG.salida.lambda2=principal.moduloDLG.MMaximav.la2;
			principal.moduloDLG.salida.lambda3=principal.moduloDLG.MMaximav.la3;
			principal.moduloDLG.salida.lambda4=principal.moduloDLG.MMaximav.la4;
			principal.moduloDLG.salida.errorReal=principal.moduloDLG.MMaximav.errorMaxDePoblacion;

			principal.salida=principal.moduloDLG.salida;
			try {
				SwingUtilities.invokeAndWait(new Runnable(){
					public void run() {
						setVisible(false);
						principal.getJBEjecutarM().setEnabled(true);
						principal.getJBEjecutarP().setEnabled(true);
						principal.getJBEjecutarMax().setEnabled(true);
						//
						principal.txtmaxverLa1.setText(Double.toString(principal.salida.lambda1));
						principal.txtmaxverLa2.setText(Double.toString(principal.salida.lambda2));
						principal.txtmaxverLa3.setText(Double.toString(principal.salida.lambda3));
						principal.txtmaxverLa4.setText(Double.toString(principal.salida.lambda4));
						principal.getJTexteErrorMaxVer().setText(Double.toString(principal.salida.errorReal));
					}});
			}catch (Exception e) {
				e.printStackTrace();
			}			

			break;
		}
	}

	private void initialize() {
		this.setSize(511, 176);
		this.setContentPane(getJContentPane());
		this.setTitle("Calculando parametros");
		this.setResizable(false);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			tiempoTxt = new JLabel();
			tiempoTxt.setBounds(new Rectangle(124, 14, 205, 15));
			tiempoTxt.setText("0");
			tiempo = new JLabel();
			tiempo.setBounds(new Rectangle(13, 14, 105, 15));
			tiempo.setText("tiempo restante:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getCancelar(), null);
			jContentPane.add(getProgressBar(), null);
			jContentPane.add(tiempo, null);
			jContentPane.add(tiempoTxt, null);
			jContentPane.add(getConsola(), null);
		}
		return jContentPane;
	}

	private JButton getCancelar() {
		if (cancelar == null) {
			cancelar = new JButton();
			cancelar.setBounds(new Rectangle(207, 115, 98, 19));
			cancelar.setText("cancelar");
			cancelar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					cancelar.setEnabled(false);
					principal.moduloDLG.cancelar();
					/*
					hiloPadre=principal.moduloDLG.hiloActual;
					try {
						hiloPadre.interrupt();
					} catch (Exception d) {
						System.err.println(d.getMessage());
					}
*/					/*
					try {
						hiloPadre.join();
						fin();
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					 */
					//espera a que finalice y extrae la solucion
		/*			while (hiloPadre.isAlive()) {
						try {
							Thread.sleep(100L);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
					}
					fin();
			*/	}});
		}
		return cancelar;
	}

	private JProgressBar getProgressBar() {
		if (progressBar == null) {
			DefaultBoundedRangeModel defaultBoundedRangeModel = new DefaultBoundedRangeModel();
			defaultBoundedRangeModel.setValueIsAdjusting(true);
			progressBar = new JProgressBar(0,100);
			progressBar.setBounds(new Rectangle(13, 39, 478, 26));
			progressBar.setBackground(Color.white);
			progressBar.setForeground(new Color(0, 191, 102));
			progressBar.setFont(new Font("Dialog", Font.BOLD, 12));
			progressBar.setModel(defaultBoundedRangeModel);
			progressBar.setStringPainted(true);
		}
		return progressBar;
	}

	/**
	 * This method initializes consola	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getConsola() {
		if (consola == null) {
			consola = new JTextField();
			consola.setBounds(new Rectangle(14, 80, 477, 30));
			consola.setEditable(false);
		}
		return consola;
	}
}  //  @jve:decl-index=0:visual-constraint="10,93"
